package br.com.hstar.client.quarto;

import br.com.hstar.client.ServiceFactory;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Esse e o quarto visualmente falando, ou seja, o painel que fica com o icone
 * do estado do quarto e com o numero do quarto.
 * 
 * @author Fernando
 *
 */
public class QuartoLabel extends Composite {

	//Static -----------------------------------------------------------
	public static final String ERROR  = "../img/error2.png";
	public static final String SUCESS = "../img/success2.png";
	public static final String ALERT  = "../img/alert2.png";
	
	//Attributes -------------------------------------------------------
	private Image 			panelImage;
	private Label 		 	panelLabel;
	private String 		 	effectOptionsDescription;
	private FlowPanel	 	flowPanel;
	private QuartoUI 		quartoUI;
	
	//Constructor ------------------------------------------------------
	public QuartoLabel(String labelText, QuartoUI _quartoUI) {

		this.quartoUI = _quartoUI;
		
		flowPanel = new FlowPanel();
		flowPanel.addStyleName("effectPanel");
		VerticalPanel vertPanel = new VerticalPanel();

		initWidget(flowPanel);
		vertPanel.setSpacing(5);
		vertPanel.setWidth("100px");

		vertPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panelImage = new Image() {
			public String toString(){
				return effectOptionsDescription;
			}
		};
		panelImage.setHeight("39");
		panelImage.setWidth("40");
		panelLabel = new Label(labelText) {
			public String toString(){
				return effectOptionsDescription;
			}
		};
		panelLabel.addStyleName("panelLabel");

		final Integer num = quartoUI.getNumero();
		
		panelImage.addClickListener(new ClickListener(){
			public void onClick(Widget arg0) {
				RootPanel.get("boxContent").clear();
				RootPanel.get("boxContent").add(new QuartoEditPanel(num));
			}
		});
		
		vertPanel.add(panelImage);
		vertPanel.add(panelLabel);
		flowPanel.add(vertPanel);
		
		//carrega o quarto do banco de dados
		ServiceFactory.createQuartoService().retrieveQuartoUsavel(quartoUI.getNumero(), new RetrieveAsyncCallback());
		
	}

	//Methods ---------------------------------------------------------------
	public QuartoUI getQuartoUI() {
		return quartoUI;
	}
	
	public void changeState() {
		if(quartoUI != null) {
			if(quartoUI.getSituacao().equals(QuartoUI.LIVRE)) {
				panelImage.setUrl("images/"+ERROR);
				getQuartoUI().setSituacao(new Integer(1));
			} else if(quartoUI.getSituacao().equals(QuartoUI.OCUPADO)) {
				panelImage.setUrl("images/"+ALERT);
				getQuartoUI().setSituacao(new Integer(2));
			} else {
				panelImage.setUrl("images/"+SUCESS);
				getQuartoUI().setSituacao(new Integer(0));
			}
		}
	}
	
	//carrega o quarto do banco, caso ele esteja ocupado ou sujo
	private class RetrieveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("Nao foi possivel carregar o quarto, causa: "+caught.getCause()+
					" - mensagem: "+caught.getMessage()+" -- "+caught.getStackTrace());
		}
		public void onSuccess(Object result) {
			QuartoUI quarto = (QuartoUI)result;
			if(quarto != null) {
				quartoUI.setSituacao(quarto.getSituacao());
				if(quarto.getSituacao().equals(new Integer(0))) {
					panelImage.setUrl("images/"+SUCESS);
				} else if(quarto.getSituacao().equals(new Integer(1))) {
					panelImage.setUrl("images/"+ERROR);
				} else {
					panelImage.setUrl("images/"+ALERT);
				}
			} else {
				panelImage.setUrl("images/"+SUCESS);
			}
		}
	}
}